aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/(profile)/+layout.server.ts
blob: a508de925485eaafb752c007f2c91a71b2d31bfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { get } from "$lib/utils";

export async function load({ parent }) {
  let { subject, user } = await parent();

  subject.follows = [];
  subject.followers = [];

  try {
    subject.follows = await get(`/${subject.pubkey}/follows`);
  } catch (e) {
    console.log("problem fetching follows", e);
  }

  try {
    subject.followers = await get(`/${subject.pubkey}/followers`);
  } catch (e) {
    console.log("problem fetching followers", e);
  }

  if (user) {
    user.follows = await get(`/${user.pubkey}/follows?tagsonly=true`);
  }

  return { subject, user };
}
-0800 committer Adam Soltys <[email protected]> 2023-01-12 15:44:13 -0800 migrating to redis' href='/coinos-server.git/commit/lib/register.js?h=notifications&id=c3565838644123e77881ff4f8536792f074047b9'>c3565838 ^
8efc5fb5 ^
c3565838 ^
9f29f46b ^

49883ffc ^
9f29f46b ^

c3565838 ^
2eb199b8 ^
9f29f46b ^
dce07aa9 ^



9f29f46b ^

8a114c50 ^

c3565838 ^
dce07aa9 ^
9f29f46b ^
c2464b24 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
                                       
                             
                                       
                            
                                 
                          
                               
                          
 
                                                      
                                    
                             
 
                                                      
 
                                        

                
 
                                           



                                                            

   

                        
                           
 







                                                               
 
                                           

   
                                                                

                                                  
                   
                       
 



                                                           

   

                                       
 
                               
              
  
import countries from "$lib/countries";
import config from "$config";
import { authenticator } from "otplib";
import bcrypt from "bcrypt";
import { l } from "$lib/logging";
import { v4 } from "uuid";
import { s, g } from "$lib/db";
import { got } from "got";

export default async (user, ip, requireChallenge) => {
  let { password, username } = user;
  l("registering", username);

  if (!username) throw new Error("Username required");

  username = username.replace(/ /g, "");
  let id = v4();
  user.id = id;

  let exists = await g(`user:${username}`);
  if (exists) throw new Error(`Username ${username} taken`);

  if (password) {
    user.password = await bcrypt.hash(password, 1);
  }

  user.currency = "USD";
  if (config.ipxapi) {
    let countryCode = "US";

    try {
      let r = await got(`https://ipxapi.com/api/ip?ip=${ip}`, {
        headers: { authorization: `Bearer ${config.ipxapi}` }
      }).json();
      if (r.success) ({ countryCode } = r);
    } catch (e) {
      console.log(e);
    }

    user.currency = countries[countryCode];
  }

  user.currencies = [...new Set([user.currency, "CAD", "USD"])];
  user.fiat = false;
  user.otpsecret = authenticator.generateSecret();
  user.balance = 0;
  user.migrated = true;

  let d = ip.split(".");
  ip = ((+d[0] * 256 + +d[1]) * 256 + +d[2]) * 256 + +d[3];
  if (Number.isInteger(ip)) {
    user.ip = ip;
  }

  await s(`user:${id}`, user);
  await s(`user:${user.username}`, id);

  l("new user", user.username);
  return user;
};